﻿using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Drawing.Drawing2D;

public class ImageThumbnailer
{
    private Bitmap _thumb = null;

    /// <summary>
    /// Creates the thumbnail.
    /// </summary>
    /// <param name="SourceImage">The source image.</param>
    /// <param name="Width">The width.</param>
    /// <param name="Height">The height.</param>
    /// <param name="KeepRatio">if set to <c>true</c> [keep ratio].</param>
    /// <returns></returns>
    public Bitmap CreateThumbnail(Bitmap SourceImage,
        Int32 Width, Int32 Height, Boolean KeepRatio)
    {
        // if Source Bitmap smaller than designated thumbnail => Return Original
        if (SourceImage.Width < Width && SourceImage.Height < Height)
            return SourceImage;

        try
        {
            Int32 _Width = 0;
            Int32 _Height = 0;

            _Width = Width;
            _Height = Height;

            if (KeepRatio)
            {
                if (SourceImage.Width > SourceImage.Height)
                {
                    _Width = Width;
                    _Height = (Int32)(SourceImage.Height *
                        ((Decimal)Width / SourceImage.Width));
                }
                else
                {
                    _Height = Height;
                    _Width = (Int32)(SourceImage.Width *
                        ((Decimal)Height / SourceImage.Height));
                }
            }

            _thumb = new Bitmap(_Width, _Height);
            using (Graphics g = Graphics.FromImage(_thumb))
            {
                g.InterpolationMode = InterpolationMode.HighQualityBicubic;
                g.FillRectangle(Brushes.White, 0, 0, _Width, _Height);
                g.DrawImage(SourceImage, 0, 0, _Width, _Height);
            }
        }
        catch
        {
            _thumb = null;
        }
        return _thumb;
    }

    /// <summary>
    /// Saves the Bitmap to the specified stream.
    /// </summary>
    /// <param name="stream">The stream.</param>
    public void Save(Stream stream)
    {
        // JPEG Optimizing
        EncoderParameters encoderParams = new EncoderParameters();
        long[] quality = new long[1];
        quality[0] = 75;
        EncoderParameter encoderParam = new EncoderParameter(Encoder.Quality, quality);
        encoderParams.Param[0] = encoderParam;

        ImageCodecInfo[] encoders = ImageCodecInfo.GetImageEncoders();
        //ImageCodecInfo codec = encoders.Where(
        //    p => p.FormatDescription.Equals("JPEG")).SingleOrDefault();

        // Save to Specified Stream
        //_thumb.Save(stream, codec, encoderParams);
    }
}